define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jbox');
	
	exports.options = {
			listLink : '',
			listParams : '',
			pageLink : '',
			pageParams : ''
		};
	
	var id = "1";
	var s = "8";
	var p = "1";
	var flag = '';
	var key = '';
	var uid = $g_user.login;
	var sum_gather = 0;
	var sum = 0;
	var categoryListData=new Array();

	var p_status = '3';
	if ($g_user.login == 'true') {
		var uid = '' + $g_user.id;
		if ($g_user.role == 'charitarian') {
			var p_status = '2';

		} else if ($g_user.role == 'admin') {
			var p_status = '1';
		} else {
			var p_status = '3';
		}
	} else {
		uid = '-1';
		var p_status = '3';
	}
	
	exports.options.listLink = $CONFIG.base_url + "/api/system/project/categorylist";
	exports.options.listParams = {"type":2};	
	var categoryList = $.get(exports.options.listLink,exports.options.listParams);	
	categoryList.done(function(data){
		//console.log(data);
		categoryListData=data;
		//data=data.slice(0,4);
		$.each(data,function(key,value){
			var len = data.length;
			if(key == len - 1){
				var a =$('<a key="3" data=" ' + value.id + '" class="project_kind" href="#' + value.categoryName + 
						'">' + value.categoryName +'</a>');
			}else if(key == 0 ){
				var a =$('<a key="3" data=" ' + value.id + '" style = "margin-left:20px;" class="project_kind" href="#' + value.categoryName + 
						'">' + value.categoryName +'</a>|');
			}else {
				var a =$('<a key="3" data=" ' + value.id + '" class="project_kind" href="#' + value.categoryName + 
						'">' + value.categoryName +'</a>|');
			}
			
			$('#open-category').append(a);
		});
		$.each(data,function(key,value){
			var tab_project = $('<div class="wrap2"><div class="hot-project-box">' +
					'<!-- title --><div class="mod-title clearfix">' +
					'<div id="FlHead"><a key="3" data="'+value.id+'" class="project_kind pull-left" name="'+value.categoryName+'">'
					+value.categoryName+'</a></div><div class="title-sub" id="sum'+ value.id + '"></div></div>'+
					'<!-- 文字介绍 --><div class="titdd-article">'+value.categoryDesc+'</div><!-- hot project --><div class="hot-project" >'+
					'<div class="pro-handle" id="kind'+value.id+'"></div></div><div class="project-more project-more-'+value.id+'"><a  class="more-btn more">'+
					'点击查看更多 >></a><a  class="more-btn less" style="display:none;"><< 收起</a></div></div></div>');
			$("#tab").append(tab_project);
			
		});
		$.each(data,function(key,value){
			var loadProjects = $.get( $CONFIG.base_url + "/api/preferential/listpage",{"projectCategoryId":value.id,"status":"1"});
			loadProjects.done(function(projectList){
				if(projectList.result.length<=4){
					$(".project-more-"+value.id).hide();
				}
				projectList.result=projectList.result.slice(0,4);
				//sum = projectList.totalCount;
				var span=$("<span>共 " + sum + "个活动</span>");
				var sum_tar = "#sum" + value.id;
				$(sum_tar).empty().append(span);
				sum_gather +=sum;
				var project =  template.render('project-template',projectList);
				var target="#kind" + value.id;
				$(target).empty().append(project);
				
				var span1 = $("<span>共"+sum_gather+"个活动</span>");
				$("#sum_gather").empty().append(span1);
			});
		});
	});
	
	//点击按钮查看更多
	$(".more").live("click",function(){
		//$("#more").live(function(){
			var value=categoryListData[$(this).parents(".wrap2").index()];
			var loadProjects = $.get( $CONFIG.base_url + "/api/preferential/listpage",{"projectCategoryId":value.id,"status":"1"});
			loadProjects.done(function(projectList){
				sum = projectList.totalCount;
				var span=$("<span>共 " + sum + "个活动</span>");
				var sum_tar = "#sum" + value.id;
				$(sum_tar).empty().append(span);
				sum_gather +=sum;
				var project =  template.render('project-template',projectList);
				var target="#kind" + value.id;
				$(target).empty().append(project);
				
				var span1 = $("<span>共"+sum_gather+"个活动</span>");
				$("#sum_gather").empty().append(span1);
			});

		$(this).hide();
		$(this).next().show();
	});
	//点击按钮收起更多
	$(".less").live("click",function(){
			var value=categoryListData[$(this).parents(".wrap2").index()];
			var loadProjects = $.get( $CONFIG.base_url + "/api/preferential/listpage",{"projectCategoryId":value.id,"status":"1"});
			loadProjects.done(function(projectList){
				sum = projectList.totalCount;
				projectList.result=projectList.result.slice(0,4);
				var span=$("<span>共 " + sum + "个活动</span>");
				var sum_tar = "#sum" + value.id;
				$(sum_tar).empty().append(span);
				sum_gather +=sum;
				var project =  template.render('project-template',projectList);
				var target="#kind" + value.id;
				$(target).empty().append(project);
				
				var span1 = $("<span>共"+sum_gather+"个活动</span>");
				$("#sum_gather").empty().append(span1);
			});

		$(this).hide();
		$(this).prev().show();
	});
	
	//轮播
	exports.loadScroll=function(){
		var hotProject = new List();
		hotProject.options.template = "carousel-template";
		hotProject.options.container = "#banner-carousel";
		hotProject.options.links = {"list" : $CONFIG.base_url + "/api/preferential/listpage"};
		hotProject.options.params = {"p" : "1","s" : "4","tags" : "hot"};
		hotProject.load(function(){
			$("#banner-carousel .item:first").addClass("active");
		});
	};
	exports.loadScroll();
	
	// 回到顶端按钮
	$(window).scroll(function() {
		var scrollTop = $(document).scrollTop();
		if (scrollTop > 0) {
			$(".back_top").show();
		} else {
			$(".back_top").hide();
		}
	});
});